package dao;

import entity.Student;

import java.sql.*;
import java.util.*;

public class StudentDao {
    //查询全部
    public List<Student> queryStudent() {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("select * from Student");
            rs = ps.executeQuery();
            List<Student> list = new ArrayList<>();
            while (rs.next()) {
                Student obj = new Student();
                obj.setId(rs.getInt("id"));
                obj.setName(rs.getString("name"));
                obj.setSex(rs.getString("sex"));
                obj.setAge(rs.getInt("age"));
                obj.setBri(rs.getDate("bri"));
                list.add(obj);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(rs, ps, conn);
        }
        return null;
    }

    //单个查询
    public Student queryStudentById(Integer id) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("select * from Student where id=?");
            ps.setInt(1, id);
            rs = ps.executeQuery();
            if (rs.next()) {
                Student obj = new Student();
                obj.setId(rs.getInt("id"));
                obj.setName(rs.getString("name"));
                obj.setSex(rs.getString("sex"));
                obj.setAge(rs.getInt("age"));
                obj.setBri(rs.getDate("bri"));
                return obj;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(rs, ps, conn);
        }
        return null;
    }

    //添加
    public int saveStudent(Student obj) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("insert into student(name, sex, age, bri) values (?, ?, ?, ?)");
            ps.setString(1, obj.getName());
            ps.setString(2, obj.getSex());
            ps.setInt(3, obj.getAge());
            ps.setDate(4, obj.getBri());
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, ps, conn);
        }
        return 0;
    }

    //删除
    public int deleteStudentById(Integer id) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("delete from Student where id=?");
            ps.setInt(1, id);
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, ps, conn);
        }
        return 0;
    }

    //修改
    public int updateStudentById(Student obj) {
        Connection conn = BaseDao.getConn();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement("update student set name=?, sex=?, age=?, bri=? where id=?");
            ps.setString(1, obj.getName());
            ps.setString(2, obj.getSex());
            ps.setInt(3, obj.getAge());
            ps.setDate(4, obj.getBri());
            ps.setInt(5, obj.getId());
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.close(null, ps, conn);
        }
        return 0;
    }
}
